//
//  KYBubbleView.m
//  DrugRef
//
//  Created by chen xin on 12-6-6.
//  Copyright (c) 2012年 Kingyee. All rights reserved.
//

#import "AppsBBubbleAnnotationView.h"
#import "CommonUtility.h"

@implementation AppsBBubbleAnnotationView

static const float kBorderWidth = 10.0f;
static const float kEndCapWidth = 10.0f; //20.0f
static const float kMaxLabelWidth = 260.0;

@synthesize dataInfo = _dataInfo;

#pragma mark - Public

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        titleLabel = [[UILabel alloc] init];
        titleLabel.backgroundColor = [UIColor clearColor];
        [titleLabel setTextColor:[UIColor whiteColor]];
        [titleLabel setTextAlignment:1];
        [titleLabel setNumberOfLines:0];
        [titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
        [self addSubview:titleLabel];
    
        
        UIImage *imageNormal, *imageHighlighted;
        imageNormal = [[UIImage imageNamed:@"mapapi.bundle/images/icon-paopao-left.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:13];
        UIImageView *leftBgd = [[UIImageView alloc] initWithImage:imageNormal
                                                 highlightedImage:imageHighlighted];
        leftBgd.tag = 11;
        imageNormal = [[UIImage imageNamed:@"mapapi.bundle/images/icon-paopao-right.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:13];
        UIImageView *rightBgd = [[UIImageView alloc] initWithImage:imageNormal
                                                 highlightedImage:imageHighlighted];
        rightBgd.tag = 12;
        
        [self addSubview:leftBgd];
        [self sendSubviewToBack:leftBgd];
        [self addSubview:rightBgd];
        [self sendSubviewToBack:rightBgd];
    }
    return self;
}

- (BOOL)showFromRect:(CGRect)rect {
    if(!_dataInfo) {
        return NO;
    }
    NSString *address = [_dataInfo objectForKey:@"address"];
    
    titleLabel.text = address;
    titleLabel.frame = CGRectZero;
    CGRect rect1 = titleLabel.frame;
    
    CGSize size = [address sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake(kMaxLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    
    rect1.origin = CGPointMake(kBorderWidth, kBorderWidth);
    rect1.size = size;
    titleLabel.frame = CGRectMake(rect1.origin.x, rect1.origin.y, size.width,size.height);
    
    
    CGFloat longWidth = rect1.size.width;
    CGRect rect0 = self.frame;
    rect0.size.height = rect1.size.height + 4*kBorderWidth;
    rect0.size.width = longWidth + 2*kBorderWidth;
    
    self.frame = rect0;
    
    CGFloat halfWidth = rect0.size.width/2;
    UIView *image = [self viewWithTag:11];
    CGRect iRect = CGRectZero;
    iRect.size.width = halfWidth;
    iRect.size.height = rect0.size.height;
    image.frame = iRect;
    
    image = [self viewWithTag:12];
    iRect.origin.x = halfWidth;
    image.frame = iRect;
    
    return YES;
}



@end
